package 多线程案例;

import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

//阻塞队列--生产者消费者模型
public class Demo_3 {
    public static void main(String[] args) {

        BlockingDeque<Integer> deque = new LinkedBlockingDeque<>(100);
        Thread t1 = new Thread(()->{
            while(true){
                try {
                    Thread.sleep(300);
                    Random random = new Random();
                    int num = random.nextInt(101);
                    System.out.println("生产数："+ num);
                    deque.put(num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"生产者");

        t1.start();

        Thread t2 = new Thread(()->{
            while(true){
                try {
                    Thread.sleep(500);
                    int num = deque.take();
                    System.out.println("消费数:"+num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"消费者");

        t2.start();
    }
}
